﻿// LTDMC_Pos_Dlg.cpp: 实现文件
//

#include "pch.h"
#include "SakerProject.h"
#include "LTDMC_Pos_Dlg.h"
#include "afxdialogex.h"

#include "xxFileProcess.h"
#include "xxWidgetControl.h"
#include "mmFile_Paras.h"

#include "Project.h"


// CLTDMC_Pos_Dlg 对话框
CLTDMC_Pos_Dlg * m_LTDMC_Pos;

IMPLEMENT_DYNAMIC(CLTDMC_Pos_Dlg, CDialogEx)

CLTDMC_Pos_Dlg::CLTDMC_Pos_Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LTDMC_Pos, pParent)
{

}

CLTDMC_Pos_Dlg::~CLTDMC_Pos_Dlg()
{
}

void CLTDMC_Pos_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CLTDMC_Pos_Dlg, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_PosName, &CLTDMC_Pos_Dlg::OnCbnSelchangeComboPosname)
	ON_BN_CLICKED(IDC_BUTTON_Save, &CLTDMC_Pos_Dlg::OnBnClickedButtonSave)
	ON_NOTIFY(NM_CLICK, IDC_LIST_Pos, &CLTDMC_Pos_Dlg::OnNMClickListPos)
	ON_BN_CLICKED(IDC_BUTTON_Move2Pos, &CLTDMC_Pos_Dlg::OnBnClickedButtonMove2pos)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CLTDMC_Pos_Dlg 消息处理程序


BOOL CLTDMC_Pos_Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	xx::InitCB(this, IDC_COMBO_PosName, mmLTDMC.vecPosNames, CurPos);

	vector<int> vecAxis = { IDC_STATIC_Axis0, IDC_STATIC_Axis1, IDC_STATIC_Axis2, IDC_STATIC_Axis3, IDC_STATIC_Axis4, IDC_STATIC_Axis5, IDC_STATIC_Axis6, IDC_STATIC_Axis7 };
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		SetDlgItemText(vecAxis[i], mmLTDMC.vecAxisNames[i]);
	}
	for (int i = mmLTDMC.AxisNum(); i < iiLTDMC_Axis_MaxNum; i++) {
		GetDlgItem(vecAxis[i])->ShowWindow(false);

	}

	mmLTDMC.InitPosLC((CListCtrl*)GetDlgItem(IDC_LIST_Pos));

	InitCurPos();

	SetTimer(Project.nTimer_LTDMC_Pos, 50, NULL);

	return TRUE;
}

void CLTDMC_Pos_Dlg::InitCurPos() {
	CString iniField = mmLTDMC.vecPosNames[CurPos];
	CString iniCurrentPath = mmFile_Paras.iniModelPath();

	vector<int> vecAxis = { IDC_EDIT_Axis0, IDC_EDIT_Axis1, IDC_EDIT_Axis2, IDC_EDIT_Axis3, IDC_EDIT_Axis4, IDC_EDIT_Axis5, IDC_EDIT_Axis6, IDC_EDIT_Axis7 };
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		xx::WriteWidget(this, vecAxis[i], mmLTDMC.vecPosInfo[CurPos][i]);
	}
	for (int i = mmLTDMC.AxisNum(); i < iiLTDMC_Axis_MaxNum; i++) {
		GetDlgItem(vecAxis[i])->ShowWindow(false);
	}


	((CListCtrl*)GetDlgItem(IDC_LIST_Pos))->SetFocus();
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		CString cstr;
		cstr.Format(L"%d", mmLTDMC.vecPosInfo[CurPos][i]);
		((CListCtrl*)GetDlgItem(IDC_LIST_Pos))->SetItemText(CurPos, i + 1, cstr);
	}
	for (int i = 0; i < mmLTDMC.PosNum(); i++) {
		((CListCtrl*)GetDlgItem(IDC_LIST_Pos))->SetItemState(i, 0, -1);
	}
	((CListCtrl*)GetDlgItem(IDC_LIST_Pos))->SetItemState(CurPos, LVNI_FOCUSED | LVIS_SELECTED, LVNI_FOCUSED | LVIS_SELECTED);
}


void CLTDMC_Pos_Dlg::OnCbnSelchangeComboPosname()
{
	CurPos = ((CComboBox*)GetDlgItem(IDC_COMBO_PosName))->GetCurSel();
	InitCurPos();
}


void CLTDMC_Pos_Dlg::OnBnClickedButtonSave()
{
	CString iniField;
	iniField.Format(L"LTDMC_Pos_%02d... ...", CurPos);
	CString iniCurrentPath = mmFile_Paras.iniModelPath();

	vector<int> vecAxis = { IDC_EDIT_Axis0, IDC_EDIT_Axis1, IDC_EDIT_Axis2, IDC_EDIT_Axis3, IDC_EDIT_Axis4, IDC_EDIT_Axis5, IDC_EDIT_Axis6, IDC_EDIT_Axis7 };
	CString cstr;
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		cstr.Format(L"Axis_[%02d]", i);
		mmLTDMC.vecPosInfo[CurPos][i] = xx::DefaultWidget(this, vecAxis[i], iniField, cstr, iniCurrentPath, 0);
	}

	InitCurPos(); // 将更新内容显示到 ListControl
}


void CLTDMC_Pos_Dlg::OnNMClickListPos(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	
	HD_NOTIFY * phdn = (HD_NOTIFY*)pNMHDR;
	int SelPos = phdn->iItem;
	if (SelPos != -1) {
		CurPos = SelPos;
		((CComboBox*)GetDlgItem(IDC_COMBO_PosName))->SetCurSel(CurPos);
		InitCurPos();
	}

	*pResult = 0;
}


void CLTDMC_Pos_Dlg::OnBnClickedButtonMove2pos()
{
	mmLTDMC.Move2Pos(CurPos);
}


void CLTDMC_Pos_Dlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == Project.nTimer_LTDMC_Pos) {
		vector<int> vecAxis = { IDC_SHOW_Axis0, IDC_SHOW_Axis1, IDC_SHOW_Axis2, IDC_SHOW_Axis3, IDC_SHOW_Axis4, IDC_SHOW_Axis5, IDC_SHOW_Axis6, IDC_SHOW_Axis7 };
		for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
			SetDlgItemInt(vecAxis[i], mmLTDMC.Axis[i].CurPos);
		}
		for (int i = mmLTDMC.AxisNum(); i < iiLTDMC_Axis_MaxNum; i++) {
			GetDlgItem(vecAxis[i])->ShowWindow(SW_HIDE);
		}
	}

	CDialogEx::OnTimer(nIDEvent);
}


void CLTDMC_Pos_Dlg::OnCancel()
{
	KillTimer(Project.nTimer_LTDMC_Pos);

	CDialogEx::OnCancel();
}
